#region The MIT License
// The MIT License
// 
// Copyright (c) 2009 octalforty studios
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;

namespace octalforty.Componento.Shared.SemanticModel
{
    /// <summary>
    /// Contains metadata for a component.
    /// </summary>
    [DebuggerDisplay("Name: {Descriptor.Name,nq}, Version: {Descriptor.Version}")]
    public sealed class ComponentManifest
    {
        #region Private Fields
        private readonly ComponentDescriptor descriptor;
        private readonly ReadOnlyCollection<Resource> resources = new ReadOnlyCollection<Resource>(new List<Resource>());
        private readonly bool isMetamanifest;
        private readonly ReadOnlyCollection<PathMapping> pathMappings = new ReadOnlyCollection<PathMapping>(new List<PathMapping>());
        private readonly ReadOnlyCollection<ComponentReference> dependencies = new ReadOnlyCollection<ComponentReference>(new List<ComponentReference>());
        #endregion

        #region Public Properties
        /// <summary>
        /// Gets a reference to the <see cref="ComponentDescriptor"/> which describes this component.
        /// </summary>
        public ComponentDescriptor Descriptor
        {
            [DebuggerStepThrough]
            get { return descriptor; }
        }

        /// <summary>
        /// Gets a collection of <see cref="Resource"/> objects which represent resources
        /// which comprise the component.
        /// </summary>
        public ReadOnlyCollection<Resource> Resources
        {
            [DebuggerStepThrough]
            get { return resources; }
        }

        /// <summary>
        /// Gets a value which indicates whether this <see cref="ComponentManifest"/> is a metamanifest,
        /// which simply installs other components.
        /// </summary>
        public bool IsMetamanifest
        {
            [DebuggerStepThrough]
            get { return isMetamanifest; }
        }

        public ReadOnlyCollection<PathMapping> PathMappings
        {
            [DebuggerStepThrough]
            get { return pathMappings; }
        }

        public ReadOnlyCollection<ComponentReference> Dependencies
        {
            [DebuggerStepThrough]
            get { return dependencies; }
        }
        #endregion

        /// <summary>
        /// Initializes a new instance of the <see cref="ComponentManifest"/> class
        /// </summary>
        /// <param name="descriptor"></param>
        /// <param name="resources"></param>
        /// <param name="pathMappings"></param>
        /// <param name="dependencies"></param>
        public ComponentManifest(ComponentDescriptor descriptor, IEnumerable<Resource> resources, IEnumerable<PathMapping> pathMappings,
                                 IEnumerable<ComponentReference> dependencies)
        {
            this.descriptor = descriptor;
            this.resources = new ReadOnlyCollection<Resource>(new List<Resource>(resources ?? new Resource[] {}));
            this.pathMappings = new ReadOnlyCollection<PathMapping>(new List<PathMapping>(pathMappings ?? new PathMapping[] {}));
            this.dependencies = new ReadOnlyCollection<ComponentReference>(new List<ComponentReference>(dependencies ?? new ComponentReference[] {}));
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ComponentManifest"/> class
        /// with the given <paramref name="name"/> and <paramref name="version"/>.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="version"></param>
        public ComponentManifest(string name, Version version)
        {
            descriptor = new ComponentDescriptor(name, version, null, null, null);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ComponentManifest"/> class
        /// with the given <paramref name="name"/>.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="version"></param>
        /// <param name="isMetamanifest"></param>
        /// <param name="resources"></param>
        public ComponentManifest(string name, Version version, bool isMetamanifest, params Resource[] resources)
        {
            descriptor = new ComponentDescriptor(name, version, null, null, null);

            this.isMetamanifest = isMetamanifest;
            this.resources = new ReadOnlyCollection<Resource>(resources);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ComponentManifest"/> class
        /// with the given <paramref name="name"/>, <paramref name="version"/> and a list of <paramref name="resources"/>.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="version"></param>
        /// <param name="resources"></param>
        public ComponentManifest(string name, Version version, IEnumerable<Resource> resources) :
            this(name, version)
        {
            this.resources = new ReadOnlyCollection<Resource>(new List<Resource>(resources));
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ComponentManifest"/> class
        /// with the given <paramref name="name"/>, <paramref name="version"/> and a list of <paramref name="resources"/>.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="version"></param>
        /// <param name="resources"></param>
        public ComponentManifest(string name, Version version, params Resource[] resources) :
            this(name, version, new List<Resource>(resources))
        {
        }

        #region Object Members
#if DEBUG
        public override string ToString()
        {
            return string.Format("{0} {1}", Descriptor.Name, Descriptor.Version);
        }
#endif
        #endregion
    }
}